"""
Case Type   : 资源池化集群系统故障模式库
Case Name   : 索引文件无权限
Create At   : 2025/04/20
Owner       : @kyleze
Description :
    1.创建表，并写入数据
    2.创建索引
    3.获取data文件磁盘，并修改磁盘权限
    4.连接数据库
    5.恢复权限
    6.重启数据库
    7.恢复环境
Expect      :
    1.成功
    2.成功
    3.成功
    4.失败
    5.成功
    6.成功
    7.成功
History     :
"""

import os
import unittest

from testcase.utils.Constant import Constant
from testcase.utils.Logger import Logger
from testcase.utils.Common import Common
from testcase.utils.CommonSH import CommonSH

from yat.test import Node
from yat.test import macro


@unittest.skipIf(
    "dms_res" not in Common().get_sh_result(
        Node("PrimaryDbUser"),
        f"source {macro.DB_ENV_PATH};cm_ctl query -Cvidp"), "非资源池化不执行")
class SharedStorageFaultsMode(unittest.TestCase):
    def setUp(self):
        self.logger = Logger()
        self.logger.info(f"----{os.path.basename(__file__)} start----")
        self.constant = Constant()
        self.com = Common()
        self.primary_sh = CommonSH("PrimaryDbUser")
        self.primary_node = Node("PrimaryDbUser")
        self.primary_root = Node("PrimaryRoot")
        self.table_name = "t_faults_test0120"
        self.index_name = "i_faults_test0120"

    def test_faults_mode(self):
        step = "--step1:创建表，并写入数据 expect:成功--"
        self.logger.info(step)
        sql_cmd = \
            f"drop table if exists {self.table_name};" \
            f"create table {self.table_name}" \
            f"(t_id integer, t_desc character(20));" \
            f"insert into {self.table_name} values " \
            f"(generate_series(1,100000),'aaa');"
        sql_res = self.primary_sh.execut_db_sql(sql_cmd)
        self.logger.info(sql_res)
        self.assertEqual(self.constant.INSERT_SUCCESS_MSG, sql_res,
                         "执行失败:" + step)

        step = "--step2:创建索引 expect:成功--"
        self.logger.info(step)
        sql_cmd = f"create unique index {self.index_name} on " \
                  f"{self.table_name}(t_id);"
        sql_res = self.primary_sh.execut_db_sql(sql_cmd)
        self.logger.info(sql_res)
        self.assertEqual(self.constant.CREATE_INDEX_SUCCESS_MSG, sql_res,
                         "执行失败:" + step)

        step = "--step3:获取data文件磁盘，并修改磁盘权限 expect:成功--"
        self.logger.info(step)
        cmd = f'source {macro.DB_ENV_PATH};' \
              f'cat $DSS_HOME/cfg/dss_vg_conf.ini |head -n 1'
        self.logger.info(cmd)
        dss_cfg = self.primary_node.sh(cmd).result()
        self.logger.info(dss_cfg)
        data_disk_name = dss_cfg.split(":")[1].strip()
        self.logger.info(data_disk_name)
        self.logger.info('--获取data_disk_name实际指向--')
        real_disk = self.primary_root.sh(
            f"readlink -f {data_disk_name}").result()
        self.logger.info(real_disk)
        self.logger.info('--修改权限--')
        cmd = f"chmod 000 {real_disk}"
        self.logger.info(cmd)
        result = self.primary_root.sh(cmd).result()
        self.logger.info(result)
        self.assertEqual("", result, "执行失败:" + step)

        step = "----step4:连接数据库 expect:失败----"
        self.logger.info(step)
        sql_cmd = f"select * from {self.table_name};"
        sql_res = self.primary_sh.execut_db_sql(sql_cmd)
        self.logger.info(sql_res)
        self.assertIn('failed to connect', sql_res, "执行失败:" + step)

        step = "--step5:恢复权限 expect:成功--"
        self.logger.info(step)
        cmd = f"chmod 660 {real_disk}"
        self.logger.info(cmd)
        result = self.primary_root.sh(cmd).result()
        self.logger.info(result)
        self.assertEqual("", result, "执行失败:" + step)

        step = "--step6:重启数据库 expect:成功--"
        self.logger.info(step)
        self.logger.info("--重启数据库--")
        self.primary_sh.restart_db_cluster()
        status = self.primary_sh.get_db_cluster_status()
        self.assertTrue("Degraded" in status or "Normal" in status,
                        "执行失败:" + step)

        step = "--step7:恢复环境 expect:成功--"
        self.logger.info(step)
        sql_cmd = f"drop table if exists {self.table_name};"
        sql_res = self.primary_sh.execut_db_sql(sql_cmd)
        self.logger.info(sql_res)
        self.assertEqual(self.constant.DROP_TABLE_SUCCESS, sql_res,
                         "执行失败:" + step)

    def tearDown(self):
        self.logger.info(f"----{os.path.basename(__file__)} end----")
